import Vue from 'vue'
import VueI18n from 'vue-i18n'
//element-ui自带多语言配置
// import ElementLocale from 'element-ui/lib/locale'
import messages from './langs'

Vue.use(VueI18n)

// 获取浏览器语言
const navLang = navigator.language || navigator.userLanguage
const localLang = (navLang === 'zh-CN' || navLang === 'zh') ? 'zh' : 'en'

// 获取本地存储的语言
const localStorageLang = localStorage.getItem('attendance-language')

// 判断最终使用的语言
let lang = localStorageLang || localLang || 'zh'

// 创建vue-i18n实例
const i18n = new VueI18n({
  locale: lang,
  messages
})

// 配置Element UI的语言
// ElementLocale.i18n((key, value) => i18n.t(key, value))

// 导出切换语言的方法
export function setLanguage(lang) {
  i18n.locale = lang
  localStorage.setItem('attendance-language', lang)
  return lang
}

export default i18n 